package jointpart.assignment2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import shared.*;

public class SearchCmd2 {

	public static void main (String[] args) throws IOException {
		String name;

		// Check that a filename has been given as argument
		if (args.length < 1) {
			System.out.println("Usage: java SearchCmd <datafile>");
			System.exit(1);
		}

		String fileName = args[0];
		HTMLlist l = Searcher2.readHtmlList (fileName);

		if (args.length > 1){ 
			// create new benchmark test and run it
			Benchmark2 myBenchmark = new Benchmark2();
			myBenchmark.runBenchmark(l, fileName, args[1]);
		}


		// Read the file and create the linked list
		BufferedReader inuser =
				new BufferedReader (new InputStreamReader (System.in));

        // Ask for a word to search
        System.out.println ("Hit return to exit.");
        boolean quit = false;
        while (!quit) {
            System.out.print ("Search for: ");
            name = inuser.readLine(); // Read a line from the terminal
            if (name == null || name.length() == 0) {
                quit = true;
            } else {
            	//Request urls from Searcher.getUrls
            	String urls = Searcher2.getUrls(l, name);
            	if (urls.length() != 0){
            		System.out.println ("The word \""+name+"\" has been found on: \n" + urls);
            	} else {
            		System.out.println ("The word \""+name+"\" has NOT been found.");
            	}
            }            
        }
    }
}
